﻿using System;
using System.Web;
using Microsoft.AspNet.SignalR;
using prufa.Models;

namespace SignalRChat
{
    public class ChatHub : Hub
    {       
        //Adding Connection to a group
        public void join(string group)
        {
            Groups.Add(Context.ConnectionId, group);           
        }

        public void joinRisk(string group)
        {
            Groups.Add(Context.ConnectionId, group);
            Clients.Caller.players();
        }

        public void startRisk(string group)
        {
            Clients.Group(group).startR();
        }

        public void changeTurn(string group)
        {
            Clients.OthersInGroup(group).yourTurn();
        }

        public void deploy(string group, int id)
        {
            Clients.OthersInGroup(group).deployArmy(id);
        }

        public void attack(string group, int id)
        {
            Clients.OthersInGroup(group).attackSelect(id);
        }

        public void defend(string group, int id)
        {
            Clients.OthersInGroup(group).DefendSelect(id);
        }

        public void attackRoll(string group, int id, int attackr, int defendr)
        {
            Clients.OthersInGroup(group).afterAttack(id, attackr, defendr);
        }

        public void takeOver(string group, int at, int dt, int player, int aa, int ar, int dr)
        {
            Clients.OthersInGroup(group).overTake(at, dt, player, aa, ar, dr);
        }

        public void endGame(string group, string Winner, int id)
        {
            //When someone wins he sends a message to others in the same group, that is those who are in the same game instance -HÖÞ
            Clients.Group(group).endIt(Winner);
           
            DefaultConnection db = new DefaultConnection();

            //find the instance of the current game from the id -HÖÞ
            var tmp = db.Risks.Find(id);

            HighScoreRepository hr = new HighScoreRepository();

            //check if the winner is Player1 or Player2 -HÖÞ
            if (Winner == tmp.Player1.UserName)
            {
                /* check if this player1 has an existing highscore for this game(Risk),
                 * if he does then we update the existing highscore -HÖÞ                            
                 */
                if (hr.CheckIfHighScoreExists(tmp.GameID.GameId, tmp.Player1.UserId))
                {                    
                    hr.UpdateHighScore(true, tmp.GameID.GameId, tmp.Player1.UserId);
                }
                //if he doesn't then we create a higscore "row"in the database and then update it -HÖÞ
                else
                {
                    hr.CreateHighScore(tmp.GameID.GameId, tmp.Player1.UserId);
                    hr.UpdateHighScore(true, tmp.GameID.GameId, tmp.Player1.UserId);
                }
                //check if player2(the looser) has an existing highscore for this game, if so then we update it -HÖÞ
                if (hr.CheckIfHighScoreExists(tmp.GameID.GameId, tmp.Player2.UserId))
                {
                    hr.UpdateHighScore(false, tmp.GameID.GameId, tmp.Player2.UserId);
                }
                //if he doesnt then we create a highscore for him and then update it -HÖÞ
                else
                {
                    hr.CreateHighScore(tmp.GameID.GameId, tmp.Player2.UserId);
                    hr.UpdateHighScore(false, tmp.GameID.GameId, tmp.Player2.UserId);
                }

            }
            // if the winner is player 2 -HÖÞ
            else if (Winner == tmp.Player2.UserName)
            {
                //check if he(player 1 ) has an existing highscore for this game(risk) and update if he does -HÖÞ
                if (hr.CheckIfHighScoreExists(tmp.GameID.GameId, tmp.Player2.UserId))
                {
                    hr.UpdateHighScore(true, tmp.GameID.GameId, tmp.Player2.UserId);
                }
                //if he doesnt have an existing higscore then we create one for him and update it -HÖÞ
                else
                {
                    hr.CreateHighScore(tmp.GameID.GameId, tmp.Player2.UserId);
                    hr.UpdateHighScore(true, tmp.GameID.GameId, tmp.Player2.UserId);
                }

                //check if the looser(player1 in this case) has an existing highscore if he does then we update it -HÖÞ
                if (hr.CheckIfHighScoreExists(tmp.GameID.GameId, tmp.Player1.UserId))
                {

                    hr.UpdateHighScore(false, tmp.GameID.GameId, tmp.Player1.UserId);

                }
                //else we create one for him and update it -HÖÞ
                else
                {
                    hr.CreateHighScore(tmp.GameID.GameId, tmp.Player1.UserId);
                    hr.UpdateHighScore(false, tmp.GameID.GameId, tmp.Player1.UserId);
                }
            }
            //remove the instance of Risk from the database once the game has ended -HÖÞ
            db.Risks.Remove(tmp);
            //save the changes in the database -HÖÞ
            db.SaveChanges();

        }


        public void Send(string group, string name, string message)
        {
            //check if message is empty
            if (!String.IsNullOrWhiteSpace(message))
            {
                //send message to everyone in the group
                Clients.Group(group).addNewMessageToPage(name, message);
            }
        }

        public void ClickCell(string groupName, string cellId)
        {
            Clients.OthersInGroup(groupName).cellClicked(cellId);
        }

        public void Win(string groupName, string Winner, int id)
        {
            //When someone wins he sends a message to others in the same group, that is those who are in the same game instance -HÖÞ
            Clients.Group(groupName).winner(Winner);

            DefaultConnection db = new DefaultConnection();

            //finds the the right game instance from the id passed to the function -HÖÞ
            var tmp = db.Myllas.Find(id);
            HighScoreRepository hr = new HighScoreRepository();
            
            //if the winner is player 1 
            if (Winner == tmp.Player1.UserName)
            {
                //check if player 1 has an existing highscore and if he does then update it -HÖÞ
                if (hr.CheckIfHighScoreExists(tmp.GameID.GameId, tmp.Player1.UserId))
                {                 
                    hr.UpdateHighScore(true, tmp.GameID.GameId, tmp.Player1.UserId);
                }
                //if hes doesn't have an existing highscore then we create one for him and then update it -HÖÞ
                else
                {
                    hr.CreateHighScore(tmp.GameID.GameId, tmp.Player1.UserId);
                    hr.UpdateHighScore(true, tmp.GameID.GameId, tmp.Player1.UserId);
                }
                //here we check if player 2(the looser in this case) has an existing highscore and update it if he does -HÖÞ
                if (hr.CheckIfHighScoreExists(tmp.GameID.GameId, tmp.Player2.UserId))
                {
                    hr.UpdateHighScore(false, tmp.GameID.GameId, tmp.Player2.UserId);
                }

                 //else we create a highscore for him and then update it   -HÖÞ
                else
                {
                    hr.CreateHighScore(tmp.GameID.GameId, tmp.Player2.UserId);
                    hr.UpdateHighScore(false, tmp.GameID.GameId, tmp.Player2.UserId);
                }
                        
            }
            //if the winner is player2 -HÖÞ
            else if (Winner == tmp.Player2.UserName)
            {
                //check if he has an existing highscore if he does then update it -HÖÞ
                if (hr.CheckIfHighScoreExists(tmp.GameID.GameId, tmp.Player2.UserId))
                {
                    hr.UpdateHighScore(true, tmp.GameID.GameId, tmp.Player2.UserId);
                }

                    //if he doesnt, then we create one for him and then update it -HÖÞ
                else
                {
                    hr.CreateHighScore(tmp.GameID.GameId, tmp.Player2.UserId);
                    hr.UpdateHighScore(true, tmp.GameID.GameId, tmp.Player2.UserId);
                }

                //here we check if player 1(the looser in this case) has an existing highscore and update it if he does -HÖÞ
                if (hr.CheckIfHighScoreExists(tmp.GameID.GameId, tmp.Player1.UserId))
                {

                    hr.UpdateHighScore(false, tmp.GameID.GameId, tmp.Player1.UserId);

                }
                //else we create one for him and then update it -HÖÞ
                else
                {
                    hr.CreateHighScore(tmp.GameID.GameId, tmp.Player1.UserId);
                    hr.UpdateHighScore(false, tmp.GameID.GameId, tmp.Player1.UserId);
                }
            }

            //remove the current game instance from the database -HÖÞ
            db.Myllas.Remove(tmp);
            //save changes to the database -HÖÞ
            db.SaveChanges();

        }
    
        public void draw(string groupName)
        {
            Clients.Group(groupName).drawer();
        }

        public void start(string groupName)
        {
            Clients.Group(groupName).startGame();
                    
        }
    }
}